package com.ellecity06.common.base

import androidx.annotation.DrawableRes
import androidx.fragment.app.Fragment
import com.ellecity06.common.R
import com.ellecity06.common.widgets.dialog.LoadingDialog

/**
 * @author ellecity06
 * @time 2023/3/29 14:30
 * @des fragment的顶级基类
 */
open class BaseFragment :Fragment(){

    private var loadingDialog: LoadingDialog? = null

    fun showLoading(text: String = "正在加载...", onCancel: (() -> Unit)? = null) {
        if (loadingDialog == null) loadingDialog = LoadingDialog(requireActivity())
        if (onCancel != null) {
            loadingDialog?.setCancelable(true)
            loadingDialog?.setOnCancelListener {
                onCancel.invoke()
            }
        }
        loadingDialog.takeIf { !requireActivity().isFinishing }?.show(text)
    }

    fun dismissLoading() {
        loadingDialog?.takeIf { it.isShowing }?.dismiss()
    }

    fun successLoading(@DrawableRes resId: Int = R.mipmap.icon_eye_open, text: String = "加载成功！", delay: Long = 2000) {
        loadingDialog?.success(resId = resId, text = text, delay = delay)
    }

    fun failLoading(@DrawableRes resId: Int = R.mipmap.icon_eye_open, text: String = "加载失败！", delay: Long = 2000) {
        loadingDialog?.fail(resId = resId, text = text, delay = delay)
    }

}
